package com.zhongnan.demo8;

import java.util.concurrent.atomic.AtomicInteger;

public class MyAtomic {
    //  int get                      获取值
    //  int getAndIncrement          以原子性将当前值加1，注意，返回加前的值
    //  int incrementAndGet          以原子性将当前值加1，注意，返回加后的值
    //  int addAndGet(int num)       以原子性将参数与当前值相加，并返回
    //  int getAndSet(int num)       以原子性将当前值替换为num，并返回当前值

    public static void main(String[] args) {
        AtomicInteger integer = new AtomicInteger(10);
        int i = integer.get();
        System.out.println(i);

        AtomicInteger integer1 = new AtomicInteger(10);
        int i1 = integer1.getAndIncrement();
        int i2 = integer1.get();
        System.out.println(i1);
        System.out.println(i2);

        AtomicInteger integer2 = new AtomicInteger(10);
        int i3 = integer2.incrementAndGet();
        int i4 = integer2.get();
        System.out.println(i3);
        System.out.println(i4);

        AtomicInteger integer3 = new AtomicInteger(10);
        int i5 = integer3.addAndGet(20);
        int i6 = integer3.get();
        System.out.println(i5);
        System.out.println(i6);

        AtomicInteger integer4 = new AtomicInteger(10);
        int i7 = integer4.getAndSet(20);
        int i8 = integer4.get();
        System.out.println(i7);
        System.out.println(i8);
    }
}
